package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CompanyDao;
import com.itheima.domain.store.Company;
import com.itheima.service.store.CompanyService;
import com.itheima.factory.DaoInstanceFactory;

import java.util.List;
import java.util.UUID;

public class CompanyServiceImpl implements CompanyService {


    @Override
    public PageInfo findAll(String pageStr, String sizeStr) {
        CompanyDao dao = DaoInstanceFactory.getMapper(CompanyDao.class);
        Integer page = 1;
        try {
            page = Integer.parseInt(pageStr);
            //处理没有传递参数,获取参数有误
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        Integer size = 5;
        try {
            Integer.parseInt(sizeStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        PageHelper.startPage(page, size);
        List<Company> all = dao.findAll();
        PageInfo<Company> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }

    @Override
    public void save(Company company) {
        CompanyDao dao = DaoInstanceFactory.getMapper(CompanyDao.class);
        String id = UUID.randomUUID().toString().replace("-", "");
        company.setId(id);
        dao.save(company);
    }

    @Override
    public Company findById(String id) {
        CompanyDao dao = DaoInstanceFactory.getMapper(CompanyDao.class);
        return dao.findById(id);
    }

    @Override
    public void delete(String id) {
        CompanyDao dao = DaoInstanceFactory.getMapper(CompanyDao.class);
        dao.delete(id);
    }

    @Override
    public List<Company> findAll() {
        CompanyDao dao = DaoInstanceFactory.getMapper(CompanyDao.class);
        return dao.findAll();
    }

    @Override
    public void update(Company company) {
        CompanyDao dao = DaoInstanceFactory.getMapper(CompanyDao.class);
        dao.update(company);
    }


}
